package com.guofen.controller.shlj;

import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.pojo.ShljEquipmentMaintenance;
import com.guofen.service.shlj.EquipmentMaintenanceService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RequestMapping("/shlj/equipmentMaintenance")
@Controller
@Api(tags = "EquipmentMaintenanceController", description = "设备保养计划及记录表")
public class EquipmentMaintenanceController {

    @Autowired
    private EquipmentMaintenanceService service;

    //保存
    @ApiOperation("添加记录_记录人")
    @RequestMapping(value = "/add_recorder",method = RequestMethod.POST)
    @ResponseBody
    public Result add_recorder(@ApiParam(name="equipmentName",value="设备名称",required=true) @RequestParam String equipmentName,
                      @ApiParam(name="operator",value="操作人",required=true) @RequestParam String operator,
                      @ApiParam(name="method",value="保养方法",required=true) @RequestParam String method,
                      @ApiParam(name="remarks",value="备注",required=true) @RequestParam String remarks,
                      @ApiParam(name="result",value="检查结果",required=true) @RequestParam String result){

        if (service.add(equipmentName,operator,method,null,result,remarks))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("添加记录_检察人")
    @RequestMapping(value = "/add_examiner",method = RequestMethod.POST)
    @ResponseBody
    public Result add_examiner(@ApiParam(name="id",value="id",required=true) @RequestParam Integer id,
                               @ApiParam(name="examiner",value="检查人",required=true) @RequestParam String examiner,
                               @ApiParam(name="result",value="检查结果",required=true) @RequestParam String result){

        if (service.update(id,null,null,null,examiner,result,null))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("根据id获取记录")
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public Result get(@ApiParam(name="id",value="设备id",required=true) @RequestParam Integer id){
        return  ResultGenerator.genSuccessResult(service.get(id));
    }

    @ApiOperation("根据名称和保养方法获取最近一周的最新记录")
    @RequestMapping(value = "/getlatest",method = RequestMethod.POST)
    @ResponseBody
    public Result getlatest(@ApiParam(name="equipmentName",value="设备名称",required=true) @RequestParam String equipmentName,
                            @ApiParam(name="maintenanceContent",value="设备保养方法",required=true) @RequestParam String maintenanceContent){
        return  ResultGenerator.genSuccessResult(service.getlatest(equipmentName,maintenanceContent));
    }

    @ApiOperation("检查设备是否保养完毕")
    @RequestMapping(value = "/is_latest_complete",method = RequestMethod.POST)
    @ResponseBody
    public Result is_latest_complete(@ApiParam(name="equipmentName",value="设备名称",required=true) @RequestParam String equipmentName,
                                     @ApiParam(name="menstanceNum",value="保养方法数量",required=true) @RequestParam String menstanceNum){
        return  ResultGenerator.genSuccessResult(service.is_latest_complete(equipmentName,menstanceNum));
    }

    @ApiOperation("检查多个设备是否保养完毕")
    @RequestMapping(value = "/is_lastweek_complete",method = RequestMethod.POST)
    @ResponseBody
    public Result is_lastweek_complete(@ApiParam(name="equipmentsName",value="设备名称",required=true) @RequestParam String equipmentsName,
                                     @ApiParam(name="menstanceNum",value="保养方法数量",required=true) @RequestParam String menstanceNum,
                                       @ApiParam(name="role",value="角色(operator/examiner)",required=true) @RequestParam String role){
        return  ResultGenerator.genSuccessResult(service.is_lastweek_complete(equipmentsName,menstanceNum,role));
    }

    @ApiOperation("获取 当日 最新 一条记录")
    @RequestMapping(value = "/getlast",method = RequestMethod.GET)
    @ResponseBody
    public Result get(){
        return  ResultGenerator.genSuccessResult(service.getlast());
    }

    @ApiOperation("获取本周已填写的保养信息的数量")
    @RequestMapping(value = "/getInThisWeek", method = RequestMethod.GET)
    @ResponseBody
    public Result getInThisWeek() {
        return ResultGenerator.genSuccessResult(service.getInThisWeek());
    }

    @ApiOperation("获取 当周 某设备的所有保养记录")
    @RequestMapping(value = "/getWeekRecords",method = RequestMethod.POST)
    @ResponseBody
    public Result getMonthRecords(@ApiParam(name="equipment_name",value="设备名称",required=true) @RequestParam String equipment_name){
        return  ResultGenerator.genSuccessResult(service.getWeekRecords(equipment_name));
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(service.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="equipmentName",value="设备名称",required=true) @RequestParam String equipmentName,
                         @ApiParam(name="operator",value="操作人",required=true) @RequestParam String operator,
                         @ApiParam(name="method",value="保养方法",required=true) @RequestParam String method,
                         @ApiParam(name="result",value="检查结果",required=true) @RequestParam String result,
                         @ApiParam(name="examiner",value="检查人",required=true) @RequestParam String examiner,
                         @ApiParam(name="remarks",value="备注",required=true) @RequestParam String remarks) {
        ShljEquipmentMaintenance record = new ShljEquipmentMaintenance();
        record.setId(id);
        record.setEquipmentName(equipmentName);
        record.setOperator(operator);
        record.setMethod(method);
        record.setResult(result);
        record.setExaminer(examiner);
        record.setRemarks(remarks);
        return ResultGenerator.genSuccessResult(service.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(service.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(service.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
